<template>
  <div>
    <el-drawer
      :visible.sync="isDrawer"
      :direction="direction"
      :size="this.$store.state.user.userIsPhone ? '90%' : '400px'"
      :with-header="false"
      @close="defaultData"
    >
      <div class="drawer-title">
        <div class="drawer-title">添加项目</div>
        <div class="iconfont icon-guanbi" @click="isDrawer = false"></div>
      </div>
      <div class="head-box">
        <div class="identifier-box"></div>
        <div>项目信息</div>
      </div>
        <el-form :model="formData" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
            <el-form-item  label="项目编号"  prop="proCode">
              <el-input v-model="formData.proCode" style="width: 92%;height: 100%;"></el-input>
            </el-form-item>
            <el-form-item label="项目简称"  prop="proShortName">
                <el-input v-model="formData.proShortName" style="width: 92%;height: 100%;"></el-input>
            </el-form-item>
            <el-form-item label="项目名称"  prop="proName">
              <el-input v-model="formData.proName" style="width: 92%;height: 100%;"></el-input>
            </el-form-item>
            <el-form-item label="当前阶段"  prop="proStage">
              <el-select v-model="formData.proStage" placeholder="请选择文件类型" filterable style="width: 92%;">
                <el-option v-for="(item, index) in stageOptions" :key="index" :label="item.dicvalue" :value="item.dickey"></el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="所在城市"  prop="proCity">
              <el-cascader v-model="formData.proCity" :options="cityList2" style="width: 92%;height: 100%;"></el-cascader>
            </el-form-item>
          </el-form>
      <div class="head-box">
        <div class="identifier-box"></div>
        <div>人员信息</div>
      </div>
      <el-form :model="formData" :rules="rules" ref="ruleForms" label-width="100px" class="demo-ruleForm">
        <el-form-item  label="销售负责人"  prop="sale">
          <el-select v-model="formData.sale" placeholder="请选择销售负责人" filterable style="width: 92%;">
            <el-option v-for="(item, index) in perOptions" :key="index" :label="item.empname" :value="item.empcode"></el-option>
           </el-select>
        </el-form-item>
        <el-form-item label="工程负责人"  >
          <el-select v-model="formData.engineering" placeholder="请选择工程负责人" filterable style="width: 92%;">
            <el-option v-for="(item, index) in perOptions" :key="index" :label="item.empname" :value="item.empcode"></el-option>
          </el-select>
        </el-form-item>
          <el-form-item label="电气负责人"  >
            <el-select v-model="formData.electrical" placeholder="请选择电气负责人" filterable style="width: 92%;">
              <el-option v-for="(item, index) in perOptions" :key="index" :label="item.empname" :value="item.empcode"></el-option>
            </el-select>
        </el-form-item>
        <el-form-item label="研发负责人"  >
          <el-select v-model="formData.research" placeholder="请选择研发负责人" filterable style="width: 92%;">
            <el-option v-for="(item, index) in perOptions" :key="index" :label="item.empname" :value="item.empcode"></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div class="head-box" style="height: 0"></div>
      <div class="btn-box">
        <div class="remove-btn" @click="cancellation('ruleForm')">取消</div>
        <div class="sub-btn" @click="determine('ruleForm')">提交</div>
      </div>
    </el-drawer>
  </div>
</template>

<script>
import { addProApi, getEmpolyeeListApi } from '@/api/index'
import cityList2 from '@/config/cityList2'

export default {
  computed: {
    cityList2 () {
      return cityList2
    }
  },
  props: {
    drawer: {
      type: Boolean,
      default: false
    },
    projectRow: {
      type: Object,
      default: undefined
    },
    getAllProjectData: {
      type: Function,
      default: undefined
    },
    stageOptions: {
      type: Array,
      default: undefined
    },
    organOptions: {
      type: Array,
      default: undefined
    },
    unitOptions: {
      type: Array,
      default: undefined
    }
  },
  data () {
    return {
      // 是否弹出抽屉
      isDrawer: false,
      // 抽屉弹出方向
      direction: 'rtl',
      formData: {
        // 项目编号
        proCode: '',
        // 项目简称
        proShortName: '',
        // 项目名称
        proName: '',
        // 项目阶段
        proStage: '',
        // 项目所在城市
        proCity: [],
        // 销售负责人
        sale: '',
        // 电气负责人
        electrical: '',
        // 工程负责人
        engineering: '',
        // 研发负责人
        research: ''
      },
      // 人员数据源
      perOptions: [],
      rules: {
        proCode: [
          { required: true, message: '请选择项目编号', trigger: 'blur' }
        ],
        proShortName: [
          { required: true, message: '请输入项目简称', trigger: 'blur' }
        ],
        proName: [
          { required: true, message: '请输入项目名称', trigger: 'blur' }
        ],
        proStage: [
          { required: true, message: '请选择项目阶段', trigger: 'blur' }
        ],
        proCity: [
          { required: true, message: '请选择项目所在城市', trigger: 'blur' }
        ],
        sale: [
          { required: true, message: '选择销售负责人', trigger: 'blur' }
        ]
      }
    }
  },

  mounted () {
    this.getEmpolyeeList()
    
  },

  methods: {
    openDrawer () {
      this.isDrawer = true
      this.formData.sale = this.$store.state.user.userCode
    },

    async getEmpolyeeList () {
      let res = await getEmpolyeeListApi()
      res.forEach((item, index) => {
        res[index].name = item.depname + '-' + item.empname
      })
      this.perOptions = [ ...res ]
    },

    // 取消新增
    cancellation (formName) {
      this.$refs['ruleForms'].resetFields()
      this.$refs[formName].resetFields()
    },
    // 确认新增
    async determine (formName) {
      this.$refs[formName].validate(async valid => {
        if (valid) {
          let data = {
            procode: this.formData.proCode,
            proname: this.formData.proName,
            proshortname: this.formData.proShortName,
            prostage: this.formData.proStage,
            proprovince: this.formData.proCity[0],
            procity: this.formData.proCity[1],
            proaddress: '',
            projectmanager: this.formData.engineering,
            electrical: this.formData.electrical,
            salescommerce: this.formData.sale,
            devleader: this.formData.research
          }
          let res = await addProApi(data)
          this.getAllProjectData()
          if (res === '添加项目成功') {
            this.$message.success('添加成功！！！')
            if (this.$store.state.user.userIsPhone) {
              this.$router.push('/MobileHomeView')
            } else {
              this.getAllProjectData()
            }
          } else {
            this.$message.success(`${res}！！！`)
          }
        } else {
          console.log('error submit!!')
          return false
        }
      })
      
    },

    defaultData () {
      this.cancellation('ruleForm')
      this.formData.proCode = ''
      this.formData.proName = ''
      this.formData.proShortName = ''
      this.formData.proStage = ''
      this.formData.sale = ''
      this.formData.electrical = ''
      this.formData.engineering = ''
      this.formData.research = ''
      this.formData.proCity = []
      this.formData.contract = ''
      this.formData.date = ''
      this.formData.amount = ''
    }
  }
}
</script>

<style lang="scss" scoped>
.main-box {
  .drawer-title {
    display: flex;
    justify-content: space-between;
    margin-top: 5px;
    margin-left: 5px;
    padding: 5px;
    padding-right: 15px;
    font-size: 16px;
    > div {
      font-weight: bold;
    }
    .icon-guanbi:hover {
      cursor: pointer;
    }
  }
  .head-box {
    display: flex;
    align-items: center;
    height: 30px;
    margin-top: 10px;
    margin-bottom: 10px;
    padding-left: 20px;
    font-size: 13px;
    line-height: 30px;
    text-align: start;
    border-bottom: 1px solid #dddcdc;
    .identifier-box {
      width: 5px;
      height: 10px;
      margin-right: 6px;
      background: #11a70c;
    }
  }
  .info-box {
    display: flex;
    padding-left: 8%;

    /* border: 1px solid red; */
    >div {
      height: 100%;

      /* border: 1px solid green; */
      >div {
        display: flex;
        align-items: center;
        justify-content: space-around;
        height: 30px;
        margin-top: 10px;
      }
    }
    .left-box {
      width: 25%;
      >div {
        justify-content: flex-end;
        font-size: 12px;
      }
    }
    .right-box {
      width: 60%;
      .file-id-box {
        overflow: hidden;
        white-space: nowrap;
        text-overflow: ellipsis;
      }
    }
  }
  .anne-box {
    display: flex;

    /* border: 1px solid red; */
    width: 100%;
    margin-top: 10px;
    padding-left: 8%;
    .upload-demo {
      /* border: 1px solid red; */
      width: 100%;
      text-align: start;
      .upload-btn {
        color: #000;
        background: rgba($color: #000, $alpha: 0%);
        border: none;
      }
      > ul::-webkit-scrollbar {
        width: 4px;
        height: 8px;
      }
      > ul::-webkit-scrollbar-thumb {
        background: fade(rgb(78 182 214), 60%);
        border-radius: 10px;
        box-shadow: inset 0 0 5px rgb(0 0 0 / 20%);
        opacity: 0.2;
      }
      > ul::-webkit-scrollbar-track {
        background: fade(rgb(78 182 214), 30%);
        border-radius: 0;
        box-shadow: inset 0 0 5px rgb(0 0 0 / 20%);
      }
    }
  }
  .btn-box {
    display: flex;
    justify-content: end;

    /* border: 1px solid #c40f0f; */
    width: 100%;
    margin: 10px 0;
    padding-right: 10px;
    padding-left: 230px;
    > div {
      width: 60px;
      height: 32px;
      margin-left: 10px;
      color: #fff;
      font-size: 12px;
      line-height: 32px;
      text-align: center;
      background: #85ce61;
      border-radius: 5px;
    }
    > div:hover {
      background: rgba($color: #85ce61, $alpha: 80%);
      cursor: pointer;
    }
    .sub-btn {
      color: #fff;
      background: #67c23a;
    }
    .sub-btn:hover {
      color: #fff;
      background: #85ce61;
      border-color: #85ce61;
      cursor: pointer;
    }
    .remove-btn {
      color: #000;
      background: rgba($color: #000, $alpha: 0%);
      border: 1px solid #dcdfe6;
    }
    .remove-btn:hover {
      color: #409eff;
      background-color: #ecf5ff;
      border-color: #c6e2ff;
      cursor: pointer;
    }
  }
}

@media screen and (width <= 768px) {
  .main-box {
    background: #ee0909;
  }
}
</style>